using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Dogfight2008
{
  public class DogfightStateMachine : IState
  {
    DFState currentState;
    Dictionary<DFState, IState> states;
    IGame game;

    public DogfightStateMachine(IGame game)
    {
      this.game = game;
      states = new Dictionary<DFState, IState>();
      states[DFState.PressFire] = new PressFireState();
      states[DFState.InGame] = new InGameState(game);
      states[DFState.GameOver] = new GameOverState();
      currentState = DFState.PressFire;
    }

    public DFState CurrentState
    {
      get
      {
        return currentState;
      }
    }

    #region IState Members

    public DFState KeyDown(Keys key)
    {
      if (states[currentState] is PressFireState)
        game.ResetGame();
      currentState = states[CurrentState].KeyDown(key);
      return currentState;
    }

    public DFState KeyUp(Keys key)
    {
      currentState = states[CurrentState].KeyUp(key);
      return currentState;
    }

    public DFState Tick(double deltaMS)
    {
      currentState = states[CurrentState].Tick(deltaMS);
      return currentState;
    }

    public void Render(IRenderer renderer)
    {
      states[currentState].Render(renderer);
    }

    #endregion

    internal void TweakConfig(TweakConfig config)
    {
      if (currentState == DFState.InGame)
      {
        InGameState gameState = states[currentState] as InGameState;
        gameState.TweakConfig(config);
      }
    }
  }
}
